#include "Videojj.h"
#include "vadbg.h"

#include <stdlib.h>
#include <string.h>

Videojj::Videojj()
{

}

Videojj::~Videojj()
{
    for (int i = 0; i < m_vjjSEI.size(); i++)
        delete m_vjjSEI[i].szUD;
}

//用户可以根据自己需要，对该函数进行修改或扩展
//这个函数的功能是往视频中写入 SEI 信息
int Videojj::process(uint8_t *pNalu, int naluLength, int timeStamp)
{
    //如果起始码后面的两个字节是 0x05 或者 0x06，那么表示 IDR 图像或者 SEI 信息
    if (pNalu[4] != 0x06 || pNalu[5] != 0x05)
        return 0;

    uint8_t *p = pNalu + 4 + 2;
    while (*p++ == 0xff);
    const char *szVideojjUUID = "VideojjLeonUUID";
    char *pp = (char *)p;

    for (int i = 0; i < strlen(szVideojjUUID); i++) {
        if (pp[i] != szVideojjUUID[i])
            return 0;
    }

    VjjSEI sei;
    sei.timeStamp = timeStamp;
    sei.length = naluLength - (pp - (char *)pNalu) - 16 - 1;
    sei.szUD = new char[sei.length];
    memcpy(sei.szUD, pp + 16, sei.length);
    m_vjjSEI.push_back(sei);

    return 1;
}
